<?php
namespace app\api\controller\v1;
use app\service\OrderService;
use app\service\PaymentService;
use app\service\TechnService;
use think\Db;
use think\Request;


class Qudao extends Base
{
	public function _initialize()
	{
		parent::_initialize();
		$this->IsLogin(); //验证登录
	}

	/**
	 * 用户信息
	 */
	public function user_qu()
    {
        $user = $this->user;
        $info = Db::name('user')->where('id',$user['id'])->find();
        $list = Db::name('user')->where('juese',3)->select();
        $data['yewulist'] = $list;
        $list = Db::name('proxy')->where('is_delete_time',0)->select();
        foreach ($list as $key=>$value){
            if ($value['is_xian'] == 1){
                $id = $value['qu'];
            }else{
                $id = $value['shi'];
            }
            $list[$key]['cityname'] = Db::name('area')->where('id',$id)->value('short_name');
        }
        $data['citylist'] = $list;
        if ($info['is_qu'] == 1){

            $data['juese'] = $info['juese'];
            return json_encode(['code'=>0,'msg'=>'是渠道','data'=>$data]);
        }else{
            $sq = Db::name('qu_apply')->where('user_id',$user['id'])->where('status',0)->find();
            if (!empty($sq)){
                return json_encode(['code'=>1,'msg'=>'申请中','data'=>$data]);
            }
            return json_encode(['code'=>-1,'msg'=>'不是渠道','data'=>$data]);

        }
    }

    public function qu_type(){
        $list = Db::name('qu_type')->where('delete_time',0)->select();
        if (!empty($list)){
            $return['data'] = $list;
            $return['code'] = 0;
            $return['msg'] = '获取成功';
        }else{
            $return['code'] = -1;
            $return['msg'] = '暂无类型';
        }
    }

    public function qu_yw_user(){
        $list = Db::name('user')->where('juese',3)->select();
        if (!empty($list)){
            $return['data'] = $list;
            $return['code'] = 0;
            $return['msg'] = '获取成功';
        }else{
            $return['code'] = -1;
            $return['msg'] = '暂无业务员';
        }

    }

    public function shen_qu(Request $request){
        $user = $this->user;
        $param = $request->param();
        if (empty($param['s_juese'])){
            echo json_encode(['code'=>-1,'msg'=>'请选择角色']);
            die;
        }
        if ($param['s_juese'] == 1 && empty($param['s_proxy_id'])){
            echo json_encode(['code'=>-1,'msg'=>'请选择地区']);
            die;
        }
//        if ($param['s_juese'] == 2 && empty($param['qu_content'])){
//            echo json_encode(['code'=>-1,'msg'=>'输入店铺名称']);
//            die;
//        }

        if ($param['s_juese'] == 1) {
            $insert['juese'] = $param['s_juese'];
            $insert['user_id'] = $user['id'];
            $techn = Db::name('techn')->where('user_id',$user['id'])->find();
            if (!empty($techn)){
                $info = Db::name('proxy')->where('qu',$techn['county'])->where('is_xian',1)->find();
                if (empty($info)){
                    $info = Db::name('proxy')->where('shi',$techn['city'])->where('is_xian',0)->find();
                }
            }else{
                $address = Db::name('user_address')->where('user_id',$user['id'])->where('is_default',1)->find();
                if (!empty($address)){
                    $info = Db::name('proxy')->where('qu',$address['county'])->where('is_xian',1)->find();
                    if (empty($info)){
                        $info = Db::name('proxy')->where('shi',$address['city'])->where('is_xian',0)->find();
                    }
                }else{
                    $info['proxy_id'] = 0;
                }
            }

//            echo json_encode(['code'=>-1,'msg'=>$param['s_proxy_id']]);die;
            $insert['proxy_id'] = !empty($param['s_proxy_id'])?$param['s_proxy_id']:$info['proxy_id'];
        }
        if ($param['s_juese'] == 2){
            $insert['juese'] = $param['s_juese'];
            $insert['user_id'] = $user['id'];
            $insert['qu_content'] = $param['s_content'];
            $insert['g_uid'] = !empty($param['s_yewu']) ? $param['s_yewu'] : 0;
            $insert['proxy_id'] = Db::name('user')->where('id',$param['s_yewu'])->value('qu_type');
        }
        if ($param['s_juese'] == 3){
            $insert['user_id'] = $user['id'];
            $insert['qu_content'] = $param['s_content'];
            $insert['juese'] = $param['s_juese'];
            $insert['proxy_id'] = $param['s_proxy_id'];
        }


        $insert['add_time'] = time();
        $ins_id = Db::name('qu_apply')->insertGetId($insert);
        if ($ins_id){
            return json_encode(['code'=>0,'msg'=>'申请成功,等待审核']);
        }else{
            return json_encode(['code'=>-1,'msg'=>'申请失败，请重试']);

        }
    }


}